package com.example.service;

import com.example.mapper.ClickLikeMapper;
import com.example.mapper.JobSeekerMapper;
import com.example.pojo.dao.MongoIdRepository;
import com.example.pojo.dao.ProblemRepository;
import com.example.pojo.dto.ProblemDTO;
import com.example.pojo.entity.Comment;
import com.example.pojo.entity.JobSeeker;
import com.example.pojo.entity.MongoId;
import com.example.pojo.entity.Problem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 本文来演示
 *
 * @author 赵建云
 * @create 2023/10/20
 */
@Service
public class ProblemServiceImpl implements ProblemService{
    @Autowired
    private ProblemRepository problemRepository;
    @Autowired
    private JobSeekerMapper jobSeekerMapper;
    @Autowired
    private MongoIdRepository mongoIdRepository;
    @Autowired
    private ClickLikeMapper clickLikeMapper;
    @Autowired
    private CommentService commentService;

    @Override
    public List<ProblemDTO> findProblemByJrId(Integer jrId) {
        List<Problem> problems = problemRepository.findAllByJrIdOrderByLikeNum(jrId);
        List<ProblemDTO> list = new ArrayList<>();
        for (Problem problem : problems) {
            List<Comment> comments = commentService.findByProblemId(problem.getId());
            ProblemDTO problemDTO = new ProblemDTO();
            problemDTO.setId(problem.getId());
            problemDTO.setJsId(problem.getJsId());
            problemDTO.setCommentNum(comments.size());
            problemDTO.setAvatar(problem.getAvatar());
            problemDTO.setLikeNum(problem.getLikeNum());
            problemDTO.setCreateTime(problem.getCreateTime());
            problemDTO.setProblem(problem.getProblem());
            problemDTO.setJrId(problem.getJrId());
            problemDTO.setJsName(problem.getJsName());
            list.add(problemDTO);
        }
        return list;
    }

    @Override
    public int saveProblem(Integer jsId,Integer jrId,String problem) {
        JobSeeker jobSeeker =jobSeekerMapper.findJSById(jsId);
        MongoId mongoId  = mongoIdRepository.findById("6532b1421144d177747c8610").orElse(null);
        if (mongoId!=null){
            mongoId.setProblemId(mongoId.getProblemId()+1);
            mongoIdRepository.save(mongoId);
        }
        Problem pb = new Problem();
        pb.setProblem(problem);
        pb.setAvatar(jobSeeker.getAvatar());
        pb.setCreateTime(new Date());
        pb.setJsId(jsId);
        pb.setJrId(jrId);
        pb.setLikeNum(0);
        pb.setJsName(jobSeeker.getJsName());
        problemRepository.save(pb);
        return 1;
    }

    @Override
    public int addProblemLikeNum(Integer jsId, String clickId) {
        Problem problem = problemRepository.findById(clickId).orElse(null);
        if (problem!=null){
            problem.setLikeNum(problem.getLikeNum()+1);
            problemRepository.save(problem);
            clickLikeMapper.save(jsId,clickId,0);
        }
        return 1;
    }

    @Override
    public int delProblemLikeNum(Integer jsId, String clickId) {
        Problem problem = problemRepository.findById(clickId).orElse(null);
        if (problem!=null){
            problem.setLikeNum(problem.getLikeNum()-1);
            problemRepository.save(problem);
            clickLikeMapper.del(jsId,clickId,0);
        }
        return 1;
    }

    @Override
    public List<String> searchClick(Integer jsId, Integer clickIdentification) {
        List<String> list = clickLikeMapper.searchClick(jsId,clickIdentification);
        return list;
    }
}
